﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using TRPOP.Common;
using TRPOP.Config;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Plugin
{
    /// <summary>
    /// 缓存keys
    /// </summary>
    public class LogKeys
    {
        /// <summary>
        /// 日志配置文件地址
        /// </summary>
        static string SyslogPath = Common.Util.GetMapPath(SystemInfoHelper.getSystemInfo().AdminFolderName + "/config/Syslog.config");


        #region 系统日志
        /// <summary>
        /// 系统日志类型
        /// </summary>
        public enum SysLogType
        {
            /// <summary>
            /// 页面级错误
            /// </summary>
            PageError = 1,
            /// <summary>
            /// 程序级的错误
            /// </summary>
            BaseProgrameError = 2
        }
        /// <summary>
        /// 获取全部的系统日志类型
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> GetAllSysLogType()
        {
            Dictionary<int, string> dicList = new Dictionary<int, string>();
            dicList.Add((int)SysLogType.PageError, "页面运行错误");
            dicList.Add((int)SysLogType.BaseProgrameError, "程序级别的错误");
            return dicList;
        }
        /// <summary>
        /// 获取全部的操作对象操作说明
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> GetAllSysLogTypeInfo()
        {
            Dictionary<int, string> dicList = new Dictionary<int, string>();
            dicList.Add((int)SysLogType.PageError, "错误信息：{0} 错误源：{1} 堆栈信息：{2}");
            dicList.Add((int)SysLogType.BaseProgrameError, "错误信息：{0} 错误源：{1} 堆栈信息：{2}");
            return dicList;
        }
        /// <summary>
        /// 根据类型获取系统日志类型的文字说明
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValueSysLogType(int key)
        {
            return BaseGetValue(GetAllSysLogType(), key);
        }
        /// <summary>
        /// 根据类型获取系统日志类型的错误说明
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValueSysLogTypeInfo(int key)
        {
            return BaseGetValue(GetAllSysLogTypeInfo(), key);
        }
        #endregion

        #region 操作类型

        /// <summary>
        /// 操作类型
        /// </summary>
        public enum OperType
        {
            /// <summary>
            /// 新增
            /// </summary>
            Insert = 1,

            /// <summary>
            /// 更新、编辑
            /// </summary>
            Update = 2,

            /// <summary>
            /// 删除
            /// </summary>
            Delete = 3
        }

        /// <summary>
        /// 获取全部的操作类型
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> GetAllOperType()
        {
            Dictionary<int, string> dicList = new Dictionary<int, string>();
            dicList.Add((int)OperType.Insert, "新增");
            dicList.Add((int)OperType.Update, "更新");
            dicList.Add((int)OperType.Delete, "删除");
            return dicList;
        }
        /// <summary>
        /// 根据类型获取操作类型的文字说明
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValueOperType(int key)
        {
            return BaseGetValue(GetAllOperType(), key);
        }
        #endregion

        #region 操作对象

        /// <summary>
        /// 操作对象
        /// </summary>
        public enum OperObject
        {
            /// <summary>
            /// 系统设置
            /// </summary>
            SysOper = 1,

            /// <summary>
            /// 结点管理
            /// </summary>
            NodeOper = 2,

            /// <summary>
            ///  信息管理
            /// </summary>
            InfoOper = 3,

            /// <summary>
            /// 内容模型管理
            /// </summary>
            ModelOper = 4,

            /// <summary>
            /// 模板编辑
            /// </summary>
            TemplateOper = 5,

            /// <summary>
            /// 会员管理
            /// </summary>
            UserOper = 6,
            /// <summary>
            /// 系统登陆
            /// </summary>
            SysLogin =7,
            /// <summary>
            /// 缓存操作
            /// </summary>
            CacheOper=8,
            /// <summary>
            /// 管理员操作
            /// </summary>
            AdminOper=9,
            /// <summary>
            /// 发布点管理
            /// </summary>
            PSNOper=10,
            /// <summary>
            /// 插件管理
            /// </summary>
            PluginOper=11


            

        }

        

        /// <summary>
        /// 获取全部的操作对象
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> GetAllOperObject()
        {
            Dictionary<int, string> dicList = new Dictionary<int, string>();
            dicList.Add((int)OperObject.SysOper, GetOperObjectKeyName((int)OperObject.SysOper));
            dicList.Add((int)OperObject.NodeOper, GetOperObjectKeyName((int)OperObject.NodeOper));
            dicList.Add((int)OperObject.InfoOper, GetOperObjectKeyName((int)OperObject.InfoOper));
            dicList.Add((int)OperObject.ModelOper, GetOperObjectKeyName((int)OperObject.ModelOper));
            dicList.Add((int)OperObject.TemplateOper, GetOperObjectKeyName((int)OperObject.TemplateOper));
            dicList.Add((int)OperObject.UserOper, GetOperObjectKeyName((int)OperObject.UserOper));
            dicList.Add((int)OperObject.SysLogin, GetOperObjectKeyName((int)OperObject.SysLogin));
            return dicList;
        }
        /// <summary>
        /// 获取Key值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string GetOperObjectKeyName(int value)
        {
            return GetConfigValue("//OperObject/item[@id='" + value + "']/name");
        }

        /// <summary>
        /// 操作结果
        /// </summary>
        public enum OperResult
        {
            /// <summary>
            /// 失败
            /// </summary>
            Failed = 0,
            /// <summary>
            /// 成功
            /// </summary>
            Success = 1,
            /// <summary>
            /// 添加
            /// </summary>
            AddSubmit = 11,
            /// <summary>
            /// 编辑
            /// </summary>
            EditSubmit = 12,
            /// <summary>
            /// 删除
            /// </summary>
            DelSubmit = 13,
            /// <summary>
            /// 添加参数类型
            /// </summary>
            AddParamType=21,
            /// <summary>
            /// 添加参数
            /// </summary>
            AddParam=22,
            /// <summary>
            /// 结点移动
            /// </summary>
            NodeMove=31,
            /// <summary>
            /// 结点恢复
            /// </summary>
            NodeRestore=32,
            /// <summary>
            /// 永久删除
            /// </summary>
            NodeDestroy=33,
            /// <summary>
            /// 清空结点回收站
            /// </summary>
            NodeRecycle=34,
            /// <summary>
            /// 结点排序
            /// </summary>
            NodeSort=35,
            /// <summary>
            ///置顶 
            /// </summary>
            InfoTopval=41,
            /// <summary>
            /// 精华
            /// </summary>
            InfoEliteval=42,
            /// <summary>
            /// 排序
            /// </summary>
            InfoSortval=43,
            /// <summary>
            /// 剪切
            /// </summary>
            InfoCut=44,
            /// <summary>
            /// 复制
            /// </summary>
            InfoCopy=45,
            /// <summary>
            /// 删除
            /// </summary>
            InfoDelete=46,
            /// <summary>
            /// 恢复
            /// </summary>
            InfoRestore=47,
            /// <summary>
            /// 永久删除
            /// </summary>
            InfoDestroy=48,
            /// <summary>
            /// 清空回收站
            /// </summary>
            InfoRecycle=49,
            /// <summary>
            /// 虚链接
            /// </summary>
            InfoCreateLink=50,
            /// <summary>
            /// 索引链接
            /// </summary>
            InfoCreateIndexLink=51,
            /// <summary>
            /// 更新发布计划
            /// </summary>
            InfoUpDatePlan=52,
            /// <summary>
            /// 添加模型字段
            /// </summary>
            ModelFieldAdd=61,
            /// <summary>
            /// 编辑模型字段
            /// </summary>
            ModelFieldEdit=62,
            /// <summary>
            /// 删除模型字段
            /// </summary>
            ModelFieldDel=63,
            /// <summary>
            /// 模型字段排序
            /// </summary>
            ModelFieldStor=64

        }


        public static Dictionary<int, string> GetAllOperResult()
        {
            Dictionary<int, string> dicList = new Dictionary<int, string>();
            dicList.Add((int)OperResult.Failed, OperResultKeyName((int)OperResult.Failed));
            dicList.Add((int)OperResult.Success, OperResultKeyName((int)OperResult.Success));
            dicList.Add((int)OperResult.AddSubmit, OperResultKeyName((int)OperResult.AddSubmit));
            dicList.Add((int)OperResult.EditSubmit, OperResultKeyName((int)OperResult.EditSubmit));
            dicList.Add((int)OperResult.DelSubmit, OperResultKeyName((int)OperResult.DelSubmit));
            dicList.Add((int)OperResult.AddParamType, OperResultKeyName((int)OperResult.AddParamType));
            dicList.Add((int)OperResult.AddParam, OperResultKeyName((int)OperResult.AddParam));
            dicList.Add((int)OperResult.NodeMove, OperResultKeyName((int)OperResult.NodeMove));
            dicList.Add((int)OperResult.NodeRestore, OperResultKeyName((int)OperResult.NodeRestore));
            dicList.Add((int)OperResult.NodeDestroy, OperResultKeyName((int)OperResult.NodeDestroy));
            dicList.Add((int)OperResult.NodeRecycle, OperResultKeyName((int)OperResult.NodeRecycle));
            dicList.Add((int)OperResult.NodeSort, OperResultKeyName((int)OperResult.NodeSort));

            dicList.Add((int)OperResult.InfoCopy, OperResultKeyName((int)OperResult.InfoCopy));
            dicList.Add((int)OperResult.InfoCreateIndexLink, OperResultKeyName((int)OperResult.InfoCreateIndexLink));
            dicList.Add((int)OperResult.InfoCreateLink, OperResultKeyName((int)OperResult.InfoCreateLink));
            dicList.Add((int)OperResult.InfoCut, OperResultKeyName((int)OperResult.InfoCut));
            dicList.Add((int)OperResult.InfoDelete, OperResultKeyName((int)OperResult.InfoDelete));
            dicList.Add((int)OperResult.InfoDestroy, OperResultKeyName((int)OperResult.InfoDestroy));
            dicList.Add((int)OperResult.InfoEliteval, OperResultKeyName((int)OperResult.InfoEliteval));
            dicList.Add((int)OperResult.InfoRecycle, OperResultKeyName((int)OperResult.InfoRecycle));
            dicList.Add((int)OperResult.InfoRestore, OperResultKeyName((int)OperResult.InfoRestore));
            dicList.Add((int)OperResult.InfoSortval, OperResultKeyName((int)OperResult.InfoSortval));
            dicList.Add((int)OperResult.InfoTopval, OperResultKeyName((int)OperResult.InfoTopval));

            return dicList;
        }
        private static string OperResultKeyName(int value)
        {
            return GetConfigValue("//OperResult/item[@id='" + value + "']/name");
        }

        /// <summary>
        /// 获取全部的操作对象操作说明
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> GetAllOperObjectInfo()
        {
            Dictionary<int, string> dicList = new Dictionary<int, string>();
            dicList.Add((int)OperObject.SysOper, GetOperObjectInfoVal((int)OperObject.SysOper));
            dicList.Add((int)OperObject.NodeOper, GetOperObjectInfoVal((int)OperObject.NodeOper));
            dicList.Add((int)OperObject.InfoOper, GetOperObjectInfoVal((int)OperObject.InfoOper));
            dicList.Add((int)OperObject.ModelOper, GetOperObjectInfoVal((int)OperObject.ModelOper));
            dicList.Add((int)OperObject.TemplateOper, GetOperObjectInfoVal((int)OperObject.TemplateOper));
            dicList.Add((int)OperObject.UserOper, GetOperObjectInfoVal((int)OperObject.UserOper));
            return dicList;
        }
        /// <summary>
        /// 获取操作说明
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string GetOperObjectInfoVal(int value)
        {
            return GetConfigValue("//OperObject/item[@id='" + value + "']/description");
        }
        /// <summary>
        /// 根据类型获取操作对象的文字说明
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValueOperObject(int key)
        {
            return BaseGetValue(GetAllOperObject(), key);
        }
        /// <summary>
        /// 根据类型获取操作对象的操作说明
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValueOperObjectInfo(int key)
        {
            return BaseGetValue(GetAllOperObjectInfo(), key);
        }
        #endregion

        #region 基本的操作方法

        /// <summary>
        /// 根据关键字获取字符串
        /// </summary>
        /// <param name="oDictionary"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string BaseGetValue(Dictionary<int, string> oDictionary, int key)
        {
            if (oDictionary.ContainsKey(key))
            {
                return oDictionary[key];
            }
            return "";
        }

        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="xpath"></param>
        /// <returns></returns>
        private static string GetConfigValue(string xpath)
        {
            return ConfigManage.GetValueByXPath(SyslogPath, xpath);
        }
        #endregion

        #region 数据库操作 程序集 名称
        /// <summary>
        /// 数据库操作 程序集 名称
        /// </summary>
        public static string DALInstance
        {
            get
            {
                string Dbtype = GetConfigValue("//DALInstance/name");
                return Dbtype.Trim();
            }
        }
        #endregion

        #region 数据库链接
        /// <summary>
        /// 数据库链接
        /// </summary>
        public static string DBConnection
        {
            get
            {
                string strConnection = GetConfigValue("//DBConnection");
                if (strConnection == "")
                    strConnection = SystemInfoHelper.getSystemInfo().CMSDataBaseConnection;
                return strConnection.Trim();
            }
        }
        #endregion
    }
}
